package com.jule.miaozhuapc.service.impl;

import java.util.Date;
import java.util.UUID;

import javax.transaction.Transactional;

import com.jule.miaozhuapc.domain.WawaPay;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jule.miaozhuapc.dao.UserDao;
import com.jule.miaozhuapc.domain.WawaUser;
import com.jule.miaozhuapc.service.LoginService;
import com.jule.miaozhuapc.util.Const;
import com.jule.miaozhuapc.util.LYHttpRequestUtil;


@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    private Logger logger = Logger.getLogger(LoginServiceImpl.class);
    @Autowired
    UserDao dao;

    /**
     * 微信 h5 登录
     */
    @SuppressWarnings({"static-access", "unused"})
    @Override
    public WawaUser Wxh5Login(String code) {
        if (code == null || "".equals(code)) return null;

        logger.info(" code " + code);
        LYHttpRequestUtil lyhttp = new LYHttpRequestUtil();
        String wxOpenId;
        String wxNickName;
        String wxGender;
        String wxPic;
        try {
            JSONObject open = lyhttp.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token?" + "appid=" + Const.WAWA_H5_WX_APPID + "&" + "secret=" + Const.WAWA_H5_WX_SECRET + "&" + "code=" + code + "&" + "grant_type=authorization_code", null);
            logger.info(open);
            JSONObject info = lyhttp.sendGet("https://api.weixin.qq.com/sns/userinfo?" + "access_token=" + open.getString("access_token") + "&" + "openid=" + open.getString("openid"), null);
            logger.info(info);

            wxOpenId = info.has("openid") ? info.getString("openid") : null;
            wxNickName = info.has("nickname") ? info.getString("nickname") : null;
            wxGender = (info.has("sex") ? info.getInt("sex") : null) == 1 ? "男" : "女";
            wxPic = info.has("headimgurl") ? info.getString("headimgurl") : null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        // check wxopenid is reg
        WawaUser wawaUser = dao.findByWxOpenId(wxOpenId);

        if (wawaUser == null || wawaUser.getId() == null) {
            wawaUser = new WawaUser();
            // 生成token
            String newToken = UUID.randomUUID().toString().replaceAll("-", "");
            wawaUser.setToken(newToken);
            wawaUser.setAddTime(new Date());
            wawaUser.setIsDelete(false);
            wawaUser.setStatus(0);

            wawaUser.setWxOpenId(wxOpenId);
            wawaUser.setWxNickName(wxNickName);
            wawaUser.setWxGender(wxGender);
            wawaUser.setWxPic(wxPic.replace("http://", "https://"));
            wawaUser.setCoin(60);
            dao.save(wawaUser);
        }
        else {
            wawaUser.setWxNickName(wxNickName);
            wawaUser.setWxGender(wxGender);
            wawaUser.setWxPic(wxPic.replace("http://", "https://"));
            dao.updateUser(wawaUser);
        }
        return wawaUser;
    }

}
